package com.qyhl.module_practice.map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.qyhl.module_practice.R;
import com.qyhl.webtv.basiclib.utils.dialog.CommonDialog;
import com.qyhl.webtv.commonlib.common.CommonUtils;

public class InfoWinAdapter implements AMap.InfoWindowAdapter, View.OnClickListener {
    private Activity mContext;
    private String titleName;
    private String snippet;
    private LatLng latLng;

    InfoWinAdapter(Activity mContext) {
        this.mContext = mContext;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        initData(marker);
        View view = initView();
        return view;
    }

    private void initData(Marker marker) {
        snippet = marker.getSnippet();
        titleName = marker.getTitle();
        latLng = marker.getPosition();
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    @NonNull
    private View initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.practice_map_info_window, null);
        TextView address = view.findViewById(R.id.address);
        TextView title = view.findViewById(R.id.title);
        TextView phone = view.findViewById(R.id.phone);

        if (CommonUtils.getInstance().getSite() == 140) {
            phone.setVisibility(View.GONE);
        }

        String[] str = snippet.split(",", 2);

        title.setText(titleName);
        address.setText("地址：" + str[0]);
        address.setOnClickListener(this);
        phone.setText("电话：" + str[1]);
        return view;
    }


    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.address) {
            try {
                new CommonDialog.Builder(mContext)
                        .setTitle("提示", R.color.global_black_lv1)
                        .setMessage("是否要跳转至高德地图，进行导航？", R.color.global_black_lv2)
                        .setPositiveButton("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                goNaviByGaoDeMap(latLng.latitude, latLng.longitude);
                            }
                        }, R.color.global_base)
                        .setNegativeButton("取消", null, R.color.global_gray_lv2).show();

            } catch (ActivityNotFoundException e) {
                Toast.makeText(mContext, "请安装高德地图！", Toast.LENGTH_SHORT).show();
            }

        }
    }

    /* func:调起高德导航
     * @param lat 纬度
     * @param lon 经度
     * @param dev 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
     * @param t  t = 0（驾车）= 1（公交）= 2（步行）= 3（骑行）= 4（火车）= 5（长途客车）
     */
    private void goNaviByGaoDeMap(Double lat, Double lon) throws ActivityNotFoundException {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        //将功能Scheme以URI的方式传入data
        Uri uri = Uri.parse("amapuri://route/plan/" +
                "?sourceApplication=" + mContext.getString(R.string.app_name)
                + "&dlat=" + lat
                + "&dlon=" + lon
                + "&dev=0"
                + "&t=0");
        intent.setData(uri);
        intent.setPackage("com.autonavi.minimap");
        mContext.startActivity(intent);
    }
}
